﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Enterprises.Wpf.Sample.UserControls.Controls
{
    /// <summary>
    /// CanvasTestView.xaml 的交互逻辑
    /// </summary>
    public partial class CanvasTestView : System.Windows.Controls.UserControl
    {
        // 定义缩放倍数
        private double zoomLevel = 1.0;
        // 定义网格线和坐标轴的相关参数
        private const double GRID_LINE_SPACING = 25;
        private const double AXIS_THICKNESS = 2;
        private const double AXIS_MARK_LENGTH = 5;
        private const double AXIS_MARK_SPACING = GRID_LINE_SPACING;
        // 定义画刷和画笔
        private SolidColorBrush gridBrush = new SolidColorBrush(Colors.LightGray);
        private SolidColorBrush axisBrush = new SolidColorBrush(Colors.Black);
        private Pen axisPen = new Pen(Brushes.Black, AXIS_THICKNESS);
        // 当图表容器大小改变时重新绘制网格线和坐标轴
        private void ChartCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DrawGridLines();
            DrawAxis();
        }
        // 处理放大按钮的点击事件
        private void ZoomInButton_Click(object sender, RoutedEventArgs e)
        {
            zoomLevel *= 2;
            ChartCanvas.LayoutTransform = new ScaleTransform(zoomLevel, zoomLevel);
            DrawAxis();
            DrawGridLines();
        }
        // 处理缩小按钮的点击事件
        private void ZoomOutButton_Click(object sender, RoutedEventArgs e)
        {
            zoomLevel /= 2;
            ChartCanvas.LayoutTransform = new ScaleTransform(zoomLevel, zoomLevel);
            DrawAxis();
            DrawGridLines();
        }
        // 处理显示/隐藏网格线按钮的点击事件
        private void GridVisibleToggle_Click(object sender, RoutedEventArgs e)
        {
            if (GridVisibleToggle.IsChecked == true)
            {
                DrawGridLines();
            }
            else
            {
                GridCanvas.Children.Clear();
            }
        }

        // 绘制网格线
        private void DrawGridLines()
        {
            GridCanvas.Children.Clear();
            if (GridVisibleToggle.IsChecked == true)
            {
                for (double x = 0; x < ChartCanvas.ActualWidth; x += GRID_LINE_SPACING * zoomLevel)
                {
                    Line line = new Line()
                    {
                        Stroke = gridBrush,
                        StrokeThickness = 1,
                        X1 = x,
                        X2 = x,
                        Y1 = 0,
                        Y2 = ChartCanvas.ActualHeight
                    };
                    GridCanvas.Children.Add(line);
                    Canvas.SetZIndex(line, -1);
                }
                for (double y = 0; y < ChartCanvas.ActualHeight; y += GRID_LINE_SPACING * zoomLevel)
                {
                    Line line = new Line()
                    {
                        Stroke = gridBrush,
                        StrokeThickness = 1,
                        X1 = 0,
                        X2 = ChartCanvas.ActualWidth,
                        Y1 = y,
                        Y2 = y
                    };
                    GridCanvas.Children.Add(line);
                    Canvas.SetZIndex(line, -1);
                }
            }
        }
        // 绘制坐标轴
        private void DrawAxis()
        {
            // 清空画布
            AxisCanvas.Children.Clear();
            // 定义坐标轴的长度和宽度
            double axisWidth = ChartCanvas.ActualWidth * zoomLevel;
            double axisHeight = ChartCanvas.ActualHeight * zoomLevel;
            // 绘制X轴
            Line xAxis = new Line()
            {
                Stroke = axisBrush,
                StrokeThickness = AXIS_THICKNESS,
                X1 = 0,
                X2 = axisWidth,
                Y1 = axisHeight / 2,
                Y2 = axisHeight / 2
            };
            AxisCanvas.Children.Add(xAxis);
            // 绘制X轴刻度
            for (double x = 0; x <= axisWidth; x += AXIS_MARK_SPACING * zoomLevel)
            {
                Line line = new Line()
                {
                    Stroke = axisBrush,
                    StrokeThickness = 1,
                    X1 = x,
                    X2 = x,
                    Y1 = axisHeight / 2 - AXIS_MARK_LENGTH,
                    Y2 = axisHeight / 2 + AXIS_MARK_LENGTH
                };
                AxisCanvas.Children.Add(line);
            }
            // 绘制Y轴
            Line yAxis = new Line()
            {
                Stroke = axisBrush,
                StrokeThickness = AXIS_THICKNESS,
                X1 = axisWidth / 2,
                X2 = axisWidth / 2,
                Y1 = 0,
                Y2 = axisHeight
            };
            AxisCanvas.Children.Add(yAxis);
            // 绘制Y轴刻度
            for (double y = 0; y <= axisHeight; y += AXIS_MARK_SPACING * zoomLevel)
            {
                Line line = new Line()
                {
                    Stroke = axisBrush,
                    StrokeThickness = 1,
                    X1 = axisWidth / 2 - AXIS_MARK_LENGTH,
                    X2 = axisWidth / 2 + AXIS_MARK_LENGTH,
                    Y1 = y,
                    Y2 = y
                };
                AxisCanvas.Children.Add(line);
            }
        }

        public CanvasTestView()
        {
            InitializeComponent();
            // 注册事件处理程序
            ChartCanvas.SizeChanged += ChartCanvas_SizeChanged;
            ZoomInButton.Click += ZoomInButton_Click;
            ZoomOutButton.Click += ZoomOutButton_Click;
            GridVisibleToggle.Click += GridVisibleToggle_Click;
            DrawAxis();
            DrawGridLines();
        }

       
    }
}
